

AddCSLuaFile( "cl_init.lua" );
AddCSLuaFile( "shared.lua" );

include( "shared.lua" );

function ENT:Initialize()

	self.Entity:PhysicsInit( SOLID_VPHYSICS );
	self.Entity:SetMoveType( MOVETYPE_VPHYSICS );
	self.Entity:SetSolid( SOLID_VPHYSICS );

	local phys = self.Entity:GetPhysicsObject();
   
	if( phys:IsValid() ) then
		phys:Wake();
	end

	self.CanUse = true;
	self.User = nil;

end

function ENT:AttachItem( item )

	if( not TS.ItemsData[item] ) then return; end

	self.Entity:SetModel( TS.ItemsData[item].Model );
	self.Entity:GetTable().ItemID = item;
	
	self.Entity:GetTable().ItemData = { }
	
	self.Entity:GetTable().ItemData.ItemEntity = self.Entity;
	
	for k, v in pairs( TS.ItemsData[item] ) do
	
		self.Entity:GetTable().ItemData[k] = TS.ItemsData[item][k];
	
	end
	
end

function ENT:ConvertToStorage()

	if( self.Entity:GetTable().ItemData ) then

		TS.ItemToContainer( self.Entity:GetTable().ItemData )

	end

end

function ENT:Think()

	if( not self.CanUse ) then
		if( self.User and self.User:IsValid() ) then
			if( not self.User:KeyDown( IN_USE ) ) then
				self.CanUse = true;
			end
		else
			self.CanUse = true;
		end
	end

end

function ENT:Use( ply )
	
	if( not self.CanUse ) then return; end
	
	if( ply:IsTied() ) then return; end 
	
	if( CurTime() - ply:GetTable().LastItemUse < .5 ) then
		return;
	end

	ply:GetTable().LastItemUse = CurTime();

	self.CanUse = false;
	self.User = ply;
	
	local trace = { }
	trace.start = ply:EyePos();
	trace.endpos = trace.start + ply:GetAimVector() * 90;
	trace.filter = ply;
	
	local tr = util.TraceLine( trace );
	
	if( self.Entity ~= tr.Entity ) then return; end
	
	if( not self.Entity:GetTable().ItemData ) then return; end
	
	CreateActionMenu( self.Entity:GetTable().ItemData.Name, ply, self.Entity:GetPos() );
		SetActionMenuEntity( self.Entity );
		
			if( string.find( self.Entity:GetTable().ItemData.Flags, "c" ) ) then
			if( not ply:HasContainer( self.Entity:GetTable().ItemData.Name ) and
				not string.find( self.Entity:GetTable().ItemData.Flags, "!" ) and not string.find( self.Entity:GetTable().ItemData.Flags, "a" ) ) then
				AddActionOption( "Put on", "eng_ampickupcontainer", k );
			end
			AddActionOption( "Look inside", "eng_amlookinsidecontainer", k );
			AddActionOption( "Break open", "eng_emptycrate", "" );
		end
		
		if( ply:IsCitizen() ) then
		
			if( string.find( self.Entity:GetTable().ItemData.Flags, "W" ) ) then
				AddActionOption( "Wear", "eng_amwearitem", k );
				AddActionOption( "Look inside", "eng_amlookinsidecontainer", k );
			end
			
		end
		
		if( ply:IsCitizen() or ply:IsCA() ) then
		
			if( string.find( self.Entity:GetTable().ItemData.Flags, "e" ) ) then
				AddActionOption( "Eat", "eng_amuseitem", k );
			end
			
		end
		
		if( string.find( self.Entity:GetTable().ItemData.Flags, "b" ) ) then
			AddActionOption( "Destroy crate", "eng_destroycrate", "" );
		end

		if( string.find( self.Entity:GetTable().ItemData.Flags, "l" ) ) then
			AddActionOption( "Read", "eng_amreaditem", k );
		end

		if( not string.find( self.Entity:GetTable().ItemData.Flags, "!" ) and not string.find( self.Entity:GetTable().ItemData.Flags, "c" ) and not self.Entity:IsConstrained() ) then
			for k, v in pairs( ply:GetTable().Inventories ) do
				if( not string.find( self.Entity:GetTable().ItemData.Flags, "s" ) or ( string.find( self.Entity:GetTable().ItemData.Flags, "s" ) and not v.IsClothes ) ) then
					if( v.Name ~= "" ) then
						if( ply:CanItemFitInInventory( k, self.Entity:GetTable().ItemID ) ) then
							AddActionOption( "Put into " .. string.lower( v.Name ), "eng_pickupitem", k );
						end
					end
				end
			end
		end
	EndActionMenu();

end

